<!-- 设备编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改设备':'添加设备'"
    :visible="visible"
    width="750px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">
      <el-row :gutter="15">
        <el-col :sm="12">
      <el-form-item label="所属组织:" prop="organization">
          <el-select
            filterable
            clearable
            v-model="form.organization"
            @change="getDepartmentsList"
            size="small"
            placeholder="-请选择所属组织-"
            class="ele-block">
            <el-option v-for="item in schoolList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
      </el-form-item>

      <el-form-item label="所属院系:" prop="superior">
          <el-select
            filterable
            clearable
            v-model="form.superior"
            @change="$forceUpdate()"
            size="small"
            placeholder="-请选择院系-"
            class="ele-block">
            <el-option v-for="item in departmentsList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
      </el-form-item>

      <el-form-item
        label="设备名称:"
        prop="name">
        <el-input
          :maxlength="20"
          v-model="form.name"
          placeholder="请输入设备名称"
          clearable/>
      </el-form-item>

      <el-form-item
        label="设备版本号:"
        prop="version">
        <el-input
          :maxlength="20"
          v-model="form.version"
          placeholder="请输入设备版本号"
          clearable/>
      </el-form-item>

      <el-form-item label="控制:"  prop="status">
        <el-radio-group
          v-model="form.status">
                  <el-radio :label="1">正常</el-radio>
                  <el-radio :label="2">停用</el-radio>
                </el-radio-group>
      </el-form-item>

      <!-- <el-form-item
        label="设备编号:"
        prop="code">
        <el-input
          :maxlength="20"
          v-model="form.code"
          placeholder="请输入设备编号"
          clearable/>
      </el-form-item>

        <el-form-item label="类型:" prop="type">
          <el-select
            filterable
            clearable
            v-model="form.type"
            size="small"
            placeholder="-请选择设备类型-"
            class="ele-block">
            <el-option v-for="item in eqtypeList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
        </el-form-item> -->
        </el-col>
        <el-col :sm="12">
      <el-form-item
        label="设备标签号:"
        prop="tagnumber">
        <el-input
          :maxlength="20"
          v-model="form.tagnumber"
          placeholder="请输入设备标签号"
          clearable/>
      </el-form-item>

      <el-form-item
        label="设备编码:"
        prop="code">
        <el-input
          :maxlength="20"
          v-model="form.code"
          placeholder="请输入设备编码"
          clearable/>
      </el-form-item>

      <el-form-item label="使用状态:" prop="use">
        <el-select
          clearable
          class="ele-block"
          v-model="form.use"
          placeholder="请选择使用状态">
                  <el-option label="使用中" :value="1"/>
                  <el-option label="已损坏" :value="2"/>
                  <el-option label="维修中" :value="3"/>
                  <el-option label="已关机" :value="4"/>
                </el-select>
      </el-form-item>

      <!-- <el-form-item
        label="设备进厂日期:"
        prop="mac">
        <el-input
          :maxlength="20"
          v-model="form.mac"
          placeholder="请输入设备进厂日期"
          clearable/>
      </el-form-item>

      <el-form-item
        label="学校所在地:"
        prop="address">
        <el-input
          :maxlength="20"
          v-model="form.address"
          placeholder="请输入学校所在地"
          clearable/>
      </el-form-item>

      <el-form-item label="设备进厂日期:">
          <el-date-picker
          type="date"
          class="ele-fluid"
          v-model="form.in_time"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          placeholder="请选择设备进厂日期"/>
      </el-form-item> -->
        <el-form-item label="设备类型:"  prop="type">
          <el-select
            clearable
            class="ele-block"
            v-model="form.type"
            placeholder="请选择使用状态">
                    <el-option label="跳绳设备" :value="1"/>
                    <el-option label="跑步设备" :value="2"/>
                    <el-option label="手环设备" :value="3"/>
                  </el-select>
        </el-form-item>
        <el-form-item label="显示顺序:" prop="sort">
          <el-input-number
            :min="0"
            v-model="form.sort"
            placeholder="请输入显示顺序"
            controls-position="right"
            class="ele-fluid ele-text-left"/>
        </el-form-item>
      </el-col>
      </el-row>

      <!-- <el-form-item
        label="调试地址:"
        prop="address">
        <el-input
          :maxlength="20"
          v-model="form.debugurl"
          placeholder="请输入设备调试地址"
          clearable/>
      </el-form-item>

      <el-form-item
        label="报修周期:"
        prop="address">
        <el-input
          :maxlength="20"
          v-model="form.repaircycle"
          placeholder="请输入设备报修周期(天)"
          clearable/>
      </el-form-item> -->

      <!-- <el-form-item label="设备图片：">
        <uploadImage :limit="1" v-model="form.img"></uploadImage>
      </el-form-item> -->







      <el-form-item label="备注:">
        <el-input
          :rows="3"
          clearable
          type="textarea"
          :maxlength="200"
          v-model="form.note"
          placeholder="请输入备注"/>
      </el-form-item>
          </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import uploadImage from '@/components/uploadImage'
export default {
  name: 'EquipmentEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {uploadImage},
  data() {
    return {
      // 表单数据
      form: Object.assign({status:1}, this.data),
      // 表单验证规则
      rules: {
              organization: [
          {required: true, message: '请选择所属组织', trigger: 'blur'}
        ],
        superior: [
          {required: true, message: '请选择使用院系', trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请输入设备名称', trigger: 'blur'}
        ],
                  code: [
          {required: true, message: '请输入设备编码', trigger: 'blur'}
        ],

        type: [
          {required: true, message: '请选择类型', trigger: 'blur'}
        ],
        version: [
          {required: true, message: '请输入设备版本号', trigger: 'blur'}
        ],
                          tagnumber: [
          {required: true, message: '请输入设备标签号', trigger: 'blur'}
        ],
        //           address: [
        //   {required: true, message: '请输入学校所在地', trigger: 'blur'}
        // ],

        status: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ],

        use: [
          {required: true, message: '请选择使用状态', trigger: 'blur'}
        ],
                          sort: [
          {required: true, message: '请输入显示顺序', trigger: 'blur'}
        ],
                  note: [
          {required: true, message: '请输入备注', trigger: 'blur'}
        ],
            },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      // 学院列表
      schoolList: [],
      // 父级院系列表
      departmentsList: [],
      // 设备类型列表
      eqtypeList: [],
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  mounted() {
    // 获取学院列表
    this.getSchoolList();
    // 获取父级院系列表
    this.getDepartmentsList();
    // 获取设备类型列表
    this.getEqtypeList();
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/equipment/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
    /**
     * 获取组织列表
     */
    getSchoolList() {
      this.$http.get('/school/getSchoolList').then(res => {
        if (res.data.code === 0) {
          this.schoolList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },

    /**
     * 获取设备类型列表
     */
    getEqtypeList() {
      this.$http.get('/equipmenttype/getEqtypeList').then(res => {
        if (res.data.code === 0) {
          this.eqtypeList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },

    /**
     * 获取院系列表
     */
    getDepartmentsList() {
      if(this.form.organization){
        this.form.superior = null;
        this.$forceUpdate();
      }
      this.$http.post('/departments/getDepartmentsList',{sid:this.form.organization}).then(res => {
        if (res.data.code === 0) {
          this.departmentsList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
  }
}
</script>

<style scoped>
</style>
